package com.jiazhong.mingxing.project.bookstore.front.config;

import com.jiazhong.mingxing.project.bookstore.front.listener.CheckStockListener;
import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RocketMQConfig {

    @Value("${rocketmq.producer.group}")
    private String producerGroup;

    @Value("${rocketmq.name-server}")
    private String nameServer;
    @Value("${rocketmq.producer.transaction_group}")
    private String transaction_group;

    @Resource
    private CheckStockListener checkStockListener;

    @Bean("rocketMQTemplate")
    public RocketMQTemplate getRocketMQTransactionTemplate() {
        RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
        TransactionMQProducer transactionMQProducer = new TransactionMQProducer();
        transactionMQProducer.setNamesrvAddr(nameServer);
        transactionMQProducer.setProducerGroup(transaction_group);
        transactionMQProducer.setTransactionListener(checkStockListener);
        rocketMQTemplate.setProducer(transactionMQProducer);
        return rocketMQTemplate;
    }

}
